package com.yododo.deploy.util;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

public class CopyListUtil {
    private static String fileName = "/copylist.txt";

    public static List<String> getCopyList(String projectDir) {

        InputStreamReader read;
        try {
            List<String> copyList = new ArrayList<String>();

            String path = CopyListUtil.class.getResource(fileName).getPath();

            read = new InputStreamReader(new FileInputStream(path), "UTF-8");
            BufferedReader reader = new BufferedReader(read);

            String line;

            while ((line = reader.readLine()) != null) {
                // line = line.replace("MODIFY /tour/trunk", "");
                // line = line.replace("ADD /tour/trunk", "");

                line = line.replace("\\", "/").replace(projectDir, "");

                if ("".equals(line.trim())) {
                    continue;
                }

                if (line.startsWith("/doc")) {
                    System.out.println("ignore dir doc");
                    continue;
                }

                copyList.add(line.trim());
            }
            try {
                reader.close();
            } catch (Exception e) {
            }
            return copyList;

        } catch (UnsupportedEncodingException e) {
            System.out.println("UnsupportedEncodingException error");
        } catch (FileNotFoundException e) {
            System.out.println("未发现copylist.txt文件");
        } catch (IOException e) {
            System.out.println("文件读写错误");
        }
        
        throw new RuntimeException("Init copy list error.");
    }
}
